<?php

namespace app\models\system;

use Yii;

/**
 * This is the model class for table "set_basic".
 *
 * @property int $id
 * @property int $order_pay_timeout 订单支付超时, 分钟
 * @property int $order_received_timeout 订单收货超时,天数
 * @property int $cash_total_for_day 提现次数/天, 天数
 * @property float $cash_min_money 提现最小金额
 * @property float $cash_service 提现服务费,百分比
 * @property string $service_mobile 客服电话
 * @property string $service_wx 客服微信
 * @property string|null $about 关于我们
 * @property string|null $agreement 注册协议
 * @property float $mini_merchant_pay 线下最低支付
 */
class SetBasic extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_basic';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_pay_timeout', 'order_received_timeout', 'cash_total_for_day'], 'integer'],
            [['cash_min_money', 'cash_service'], 'number'],
            [['about', 'agreement'], 'string'],
            [['service_mobile', 'service_wx'], 'string', 'max' => 255],

            [['mini_merchant_pay'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_pay_timeout' => 'Order Pay Timeout',
            'order_received_timeout' => 'Order Received Timeout',
            'cash_total_for_day' => 'Cash Total For Day',
            'cash_min_money' => 'Cash Min Money',
            'cash_service' => 'Cash Service',
            'service_mobile' => 'Service Mobile',
            'service_wx' => 'Service Wx',
            'about' => 'About',
            'agreement' => 'Agreement',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetBasic' => $data]) && $model->save()) {
            return true;
        }
    }
}
